package rui.game.component;

import lombok.Getter;
import lombok.Setter;
import rui.game.AboutFrame;
import rui.game.component.function.Cheat;

import javax.swing.*;
import java.awt.*;

/**
 * @author 赵睿
 */
@Setter
public class GameMenu extends JMenuBar {

    private JMenu jmAbout;
    private JMenu jmHelp;
    private Cheat cheat;
    @Getter
    private JMenuItem jmRule;

    public GameMenu(Cheat cheat) {
        this.cheat = cheat;
    }

    public static GameMenu init(GameMenu menu,Cheat cheat,GameFrame gameFrame){
        if(menu==null){
            menu = new GameMenu(cheat);
            menu.init();
            gameFrame.setJMenuBar(menu);
            initAboutRule(menu,gameFrame);
        }
        return menu;
    }

    public void init() {
        help();
        about();
        gameRule();

        this.add(jmHelp);
        this.add(jmAbout);

        combine();
    }

    public static void initAboutRule(GameMenu menu,GameFrame gameFrame){
      menu.getJmRule().addActionListener((action)->{
            AboutFrame af = new AboutFrame(gameFrame);
            af.setVisible(true);
        });
    }

    protected void combine() {
        jmHelp.add(cheat);
        jmAbout.add(jmRule);
    }

    private void help() {
        if (jmHelp != null) {
            return;
        }
        // 实例化各个菜单
        jmHelp = new JMenu("帮助");
        // 设置字体和大小
        jmHelp.setFont(new Font("楷体", Font.PLAIN, 14));
    }

    private void about() {
        if (jmAbout != null) {
            return;
        }
        jmAbout = new JMenu("关于");
        jmAbout.setFont(new Font("楷体", Font.PLAIN, 14));
    }

    private void gameRule() {
        if (jmRule != null) {
            return;
        }
        jmRule = new JMenuItem("游戏规则");
        jmRule.setFont(new Font("楷体", Font.PLAIN, 12));
    }
}
